
// #endif /* MY_INC_APP_APP_ACLoad_H_ */
/**
 * @file    ACLoad.h
 * @brief   AC负载系统控制头文件（优化版）
 * @date    2024-06-10
 * @author  Liangmiyi
 */

#ifndef MY_INC_APP_APP_ACLOAD_PWM_H_
#define MY_INC_APP_APP_ACLOAD_PWM_H_
#include "MY_INC/LC/LC_SYS.h"
// SPWM_FreqDouble_Inline 参数类型和内部访问修正
static inline void SPWM_Control_Inline(Pwm *Pwm);
static inline void SPWM_Inline(Pwm *Pwm);
static inline void SPWM_Control_Inline(Pwm *Pwm)
{
    if (Pwm->pwm_en_flag)
    {
        if (Pwm->pwm_flag == Nonactivated) // 访问 pwm_flag
        {
            Pwm->pwm_flag = Activated; // 设置为已激活状态
            LL_HRTIM_EnableOutput(HRTIM1, Pwm->output_p[0] | Pwm->output_n[0] | Pwm->output_p[1] | Pwm->output_n[1]);
        }
        SPWM_Inline(Pwm); // 调用 SPWM_Inline 计算 PWM 占空比
    }
    else
    {
        Pwm->pwm_flag = Nonactivated; // 设置为已激活状态
        LL_HRTIM_DisableOutput(HRTIM1, Pwm->output_p[0] | Pwm->output_n[0] | Pwm->output_p[1] | Pwm->output_n[1]);
    }
}
static inline void SPWM_Inline(Pwm *Pwm)
{
    Pwm->pwm_width[0] = ((int32_t)(Pwm->mod_wave * (float)PWM_PERIOD + (float)(PWM_PERIOD))) >> 1;
    Pwm->pwm_width[1] = PWM_PERIOD - Pwm->pwm_width[0]; // 计算相位N的PWM脉冲宽度
    // 访问 Pwm->pwm_width
    Pwm->pwm_width[0] = (Pwm->pwm_width[0] > U_LIMIT_HIGH) ? U_LIMIT_HIGH : Pwm->pwm_width[0];
    Pwm->pwm_width[0] = (Pwm->pwm_width[0] < U_LIMIT_ZERO) ? 0 : Pwm->pwm_width[0]; // 注意这里是0，而不是U_LIMIT_ZERO
    Pwm->pwm_width[1] = (Pwm->pwm_width[1] > U_LIMIT_HIGH) ? U_LIMIT_HIGH : Pwm->pwm_width[1];
    Pwm->pwm_width[1] = (Pwm->pwm_width[1] < U_LIMIT_ZERO) ? 0 : Pwm->pwm_width[1]; // 注意这里是0，而不是U_LIMIT_ZERO

    // 访问 Pwm->timer1/2
    LL_HRTIM_TIM_SetCompare1(HRTIM1, Pwm->timer[0], Pwm->pwm_width[0]);
    LL_HRTIM_TIM_SetCompare1(HRTIM1, Pwm->timer[1], Pwm->pwm_width[1]);
    // 必须等待PWM占空比计算完成后再使能PWM闸门，PWM占空比会延迟一个周期生效
}


#endif /* MY_INC_APP_APP_ACLOAD_H_ */
